package com.dxy.hms.controller;

import com.alibaba.excel.EasyExcel;
import com.dxy.hms.model.excel.RoomExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.dxy.hms.base.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dxy.hms.model.entity.Room;
import com.dxy.hms.service.RoomService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
* <p>
* 房间表 controller
* </p>
*
* @author dxy
* @since 2024-10-29
*/
@Slf4j
@Api(tags = "房间表接口")
@RestController
@RequestMapping("/hms/room")
public class RoomController {

    @Autowired
    private RoomService service;

    @ApiOperation("增加")
    @PostMapping("/add")
    public Resp add(@RequestBody Room entity) {
        service.add(entity);
        return Resp.bizSuccess();
    }

    @ApiOperation("根据id修改")
    @PostMapping("/updById")
    public Resp updById(@RequestBody Room entity) {
        service.updById(entity);
        return Resp.bizSuccess();
    }

    @ApiOperation("根据id删除")
    @PostMapping("/delByIds")
    public Resp delByIds(@RequestBody List<Long> ids) {
        service.delByIds(ids);
        return Resp.bizSuccess();
    }

    @ApiOperation("详情")
    @GetMapping("/getDetail")
    public Resp<Room> getDetail(Long id) {
        return Resp.bizSuccess(service.getDetail(id));
    }

    @ApiOperation("分页")
    @GetMapping("/getPage")
    public Resp<IPage<Room>> getPage(Long roomTypeId, String roomStatus, Long pageNum, Long pageSize) {
        return Resp.bizSuccess(service.getPage(roomTypeId, roomStatus, pageNum, pageSize));
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long roomTypeId, String roomStatus) throws Exception {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("客房信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<RoomExcel> list = service.findRoomExcel(roomTypeId, roomStatus);

        try(ServletOutputStream os = response.getOutputStream()) {
            EasyExcel.write(os, RoomExcel.class)
                    .sheet("客房信息")
                    .doWrite(list);
        }
    }

}